package csl.mq.rabbitmq.receive;

import cn.hutool.bloomfilter.BitMapBloomFilter;
import cn.hutool.core.date.DateUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Description: TODO 监听简单队列信息
 * @author: 隔山海
 * @date: 2021年09月05日 10:46
 */
@Slf4j
@Component
@RabbitListener(queues = {"hello.queue"})
public class NormalMessageReceive {

    @Autowired
    private BitMapBloomFilter bitMapBloomFilter;

    @RabbitHandler
    public void onMessage(String content, Message message, Channel channel){
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String messageId = message.getMessageProperties().getMessageId();
        try {
            if (bitMapBloomFilter.contains(messageId)){
                log.warn("消息重复消费！----->{}",message);
            }else{
                bitMapBloomFilter.add(messageId);
                channel.basicAck(deliveryTag,false);
                log.info("{} ---- {}:[{}]", DateUtil.now(),messageId,content);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
